/// <reference path="../../../../../../../typings/jquery/jquery.d.ts"/>
/// <reference path="../../../../../../../typings/angularjs/angular.d.ts"/>
define(['./../../initial', './../../model/Project', './../../model/StatisticInputItem', './../../service/chartFactory'],
    function () {

        var app = angular.module('App.home');

        app.controller('statistic.projectCtrl', ['$scope', '$timeout', '$rootScope', '$state', '$stateParams', 'allProjects', 'StatisticInputItem', 'curPrj', 'chartFactory', 'statisticInputItemList', 'locationList',
            function ($scope, $timeout, $rootScope, $state, $stateParams, allProjects, StatisticInputItem, curPrj, chartFactory, statisticInputItemList, locationList) {

                //初始化3种类型
                $scope.minItem = new StatisticInputItem('less');
                $scope.betweenItem = new StatisticInputItem('between');
                $scope.maxItem = new StatisticInputItem('more');
                //当前radio选择的类型
                $scope.type = "between";

                $scope.setType = function (type) {
                    $scope.type = type;
                };
                $scope.conditions = statisticInputItemList.conditions;
                $scope.conditionTitle = "查询条件:";
                statisticInputItemList.searchType = 'design';
                statisticInputItemList.changeUnit();
                $scope.addCondition = function () {
                    $('#addCondition').popup('hide');
                    switch ($scope.type) {
                        case 'less':
                            statisticInputItemList.addCondition('less', 0, $scope.minItem.max);
                            break;
                        case 'between':
                            statisticInputItemList.addCondition('between', $scope.betweenItem.min, $scope.betweenItem.max);
                            break;
                        case 'more':
                            statisticInputItemList.addCondition('more', $scope.maxItem.min);
                            break;
                        default:
                            break;
                    }
                };
                $scope.removeCondition = function (index) {
                    statisticInputItemList.removeCondition(index);
                };


                //初始化radioButtons,使用semantic插件,让样式更美观
                $timeout(function () {
                    $('.ui.radio.checkbox.checked').checkbox();
                    $('#addCondition').popup({
                        // position: 'bottom left',
                        on: 'click'
                    });
                    $('a.item.tab-item').tab();

                    $scope.chartHeight = document.documentElement.clientHeight - 250;
                    $scope.showTab = false;
                    $scope.query();
                }, 800);
                $scope.tabs = locationList.map(function (item) {
                    return {
                        title: item
                    };
                });

                $scope.query = function (timeIndex) {
                    $scope.showTab = false;
                    curPrj.postConvergence({
                        locationList: locationList,
                        conditions: $scope.conditions,
                        startTimeList: allTimeList.map(function (item) {
                            return item[timeIndex];
                        }),
                        isDesign: true,
                        endTimeList: allTimeList.map(function (item) {
                            return item[timeIndex];
                        })
                    }).then(function (data) {
                        //第一次查询后,填充时间列表
                        fillDataTimes(data);
                        $scope.datas = data;
                        $scope.showTab = true;
                        data.forEach(function (item, index) {
                            chartFactory.createStastisBar('#chart' + index, item, document.getElementById('conditionContainer').clientWidth - 60, null, 'project');
                        });
                    });
                };
                
                //记录各个location下的时间列表
                var allTimeList = [];
                //填充所有时间列表
                function fillDataTimes(data) {
                    if (data.length === 0 || ($scope.allTimes && $scope.allTimes.length > 0)) return;
                    //记录所有location的时间列表
                    allTimeList = data.map(function (item) {
                        return item.allTimes;
                    });
                    $scope.allTimes = data[0].allTimes.map(function (item, index) {
                        return {
                            time: item,
                            active: index === 0
                        };
                    });
                    $timeout(function () {
                        $('#queryBtn').dropdown();
                    }, 300);
                }
            }
        ]);
    });
